package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.com.util.DateUtils;
import jsyf.oa.dao.system.*;
import jsyf.oa.entity.system.TargetEntity;
import jsyf.oa.entity.system.User2RegionEntity;
import jsyf.oa.entity.system.UserEntity;
import jsyf.oa.facade.system.User2RegionFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Lenovo on 2017/12/9.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class User2RegionService implements User2RegionFacade {
    @Autowired
    private User2RegionDao dao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private TargetDao targetDao;
    @Autowired
    private TargetRoleDao targetRoleDao;
    @Autowired
    private RegionDao regionDao;

    @Override
    public Boolean config(String userId, String regionIds) {
        dao.removeByCondition(" AND USER_ID = ? ", new String[]{userId});
        if (!StringUtils.isEmpty(regionIds)) {
            List<String> list = BaseJdbcDao.stringToList(regionIds);
            for (int i = 0; i < list.size(); i++) {
                User2RegionEntity entity = new User2RegionEntity();
                entity.setUserId(userId);
                entity.setRegionId(list.get(i));
                dao.add(entity);
            }
            JSONObject users = userDao.getViewJSONById(userId);
            if ("区域经理".equals(users.getString("roleName"))) {
              /*  JSONObject bigUser = dao.getRegionByUserId(userId);
                UserEntity entity = (UserEntity) userDao.getIBaseEntityById(userId);
                if (bigUser != null && !"".equals(bigUser.getString("id"))) {
                    entity.setParentId(bigUser.getString("id"));
                    userDao.update(entity);
                } else {
                    entity.setParentId("");
                    userDao.update(entity);
                }*/
                //删除此人本月目标
                Date day = userDao.getCurrentTime();
                String nowMonth = DateUtils.parse(day, "yyyy-MM");
                targetDao.removeByCondition(" AND LEFT(TARGET_DATE,7) = ? AND USER_ID = ? ", new String[]{nowMonth, userId});
                //获取电访、面访目标
                List<JSONObject> targetList = targetRoleDao.getJSONListByCondition(" AND COMPANY_ID = ? AND ROLE_NAME_ID = ? ", new Object[]{users.get("companyId").toString(), users.get("roleNameId").toString()});
                Integer faceTarget = 0;
                Integer telTarget = 0;
                if (targetList.size() > 0) {
                    faceTarget = Integer.valueOf(targetList.get(0).get("faceTarget").toString());
                    telTarget = Integer.valueOf(targetList.get(0).get("telTarget").toString());
                }
                Calendar calendar = Calendar.getInstance();
                //获取前月的第一天
                String firstDay = DateUtils.getFirstDayOfMonth();
                //获取当月天数
                Integer days = DateUtils.getLastDayOfMonth(calendar.get(calendar.YEAR), calendar.get(calendar.MONTH) + 1);
                //生成本月目标
                List<TargetEntity> targetEntityList = new ArrayList<>();
                for (Integer j = 1; j <= days; j++) {
                    TargetEntity targetEntity = new TargetEntity();
                    //某天目标
                    String targetDate = firstDay.substring(0, 7) + "-" + (j < 10 ? "0" + j : j);
                    targetEntity.setUserId(userId);
                    targetEntity.setTargetDate(targetDate);
                    targetEntity.setCompanyId(users.get("companyId").toString());
                    targetEntity.setFaceTarget(faceTarget);
                    targetEntity.setTelTarget(telTarget);
                    targetEntity.setCreateUserId(userId);
                    //查询销售的所在区域id
                    JSONObject regionJson = regionDao.getRegionByRoleName(userId, " AND PARENT_ID != '0'");
                    if (regionJson != null) {
                        targetEntity.setRegionId(regionJson.get("id").toString());
                    }
                    //查询销售的大区id
                    JSONObject bigRegionJson = regionDao.getRegionByRoleName(userId, " AND PARENT_ID = '0'");
                    if (regionJson != null) {
                        targetEntity.setBigRegionId(bigRegionJson.get("id").toString());
                    }
                    targetEntityList.add(targetEntity);
                }
                targetDao.insertBatch(targetEntityList);

            }
        }
        return true;
    }

    @Override
    public List<JSONObject> getByUserId(String userId) {
        return dao.getJSONListByCondition(" AND USER_ID = ? ", new Object[]{userId});
    }
}
